/**
 * Copyright &copy; com.nmf
 */
package com.nmf.site.basedata.city.service;

import java.util.List;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.nmf.site.common.PageDb;
import org.apache.commons.lang3.StringUtils;

import com.nmf.dao.gen.bean.BaseCities;
import com.nmf.dao.gen.bean.BaseCitiesExample;
import com.nmf.dao.gen.BaseCitiesMapper;
import com.nmf.site.common.enums.DelFlagEnums;
import com.nmf.site.common.persistence.Page;
import com.nmf.site.common.utils.IdGenerater;

/**
 * 地州市Service
 * @author 北冥
 * @version 2018-12-21
 */
@Service
@Transactional(readOnly = true)
public class BaseCitiesService{

	@Autowired
	BaseCitiesMapper baseCitiesMapper;
	@Autowired
	IdGenerater idGenerater;
	
	public BaseCities get(String no) {
		return baseCitiesMapper.selectByPrimaryKey(no);
	}
	
	public List<BaseCities> findList(BaseCities baseCities) {
		BaseCitiesExample baseCitiesExample = new BaseCitiesExample();
		BaseCitiesExample.Criteria criteria = baseCitiesExample.createCriteria();
		
		return baseCitiesMapper.selectByExample(baseCitiesExample);
	}
	
	public Page<BaseCities> findPage(Page<BaseCities> page, BaseCities baseCities) {
		BaseCitiesExample baseCitiesExample = new BaseCitiesExample();
		BaseCitiesExample.Criteria criteria = baseCitiesExample.createCriteria();
		
		baseCitiesExample.setPage(page);
		
		List<BaseCities> dlist = baseCitiesMapper.selectByExample(baseCitiesExample);
		page.setList(dlist);
		page.setCount(baseCitiesMapper.countByExample(baseCitiesExample));
		
		return page;
	}
	
	@Transactional(readOnly = false)
	public void save(BaseCities baseCities) {
		if(StringUtils.isNotEmpty(baseCities.getId())&&get(baseCities.getId())!=null) {
			baseCitiesMapper.updateByPrimaryKeySelective(baseCities);
		}else {
			Date date = new Date();
			baseCities.setCreateDate(date);
			baseCities.setId(idGenerater.genClsNo("baseCities"));
			baseCities.setUpdateDate(date);
			baseCitiesMapper.insertSelective(baseCities);
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(BaseCities baseCities) {
		baseCitiesMapper.deleteByPrimaryKey(baseCities.getId());
	}
	
}